/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef PANDA_TOOLING_TEST_JS_GET_VARIABLE_TEST_H
#define PANDA_TOOLING_TEST_JS_GET_VARIABLE_TEST_H

#include "test_util.h"
#include "plugins/ecmascript/runtime/include/tooling/pt_ecmascript_extension.h"
#include "plugins/ecmascript/tests/runtime/tooling/js_test_api.h"

#include <climits>
#include <cfloat>

namespace ark::tooling::test {
using JSTaggedValue = ark::ecmascript::JSTaggedValue;

class JsGetVariableTest : public ApiTest {
public:
    JsGetVariableTest()
    {
        vmDeath = [this]() {
            const int expectedCount = 1;
            ASSERT_EQ(countChecker_, expectedCount);
            return true;
        };

        loadModule = [this](std::string_view moduleName) {
            if (moduleName.find(pandaFile_.c_str()) == std::string_view::npos) {
                return true;
            }
            // NOLINTNEXTLINE(readability-magic-numbers)
            auto location = TestUtil::GetLocation("GetVariable.js", 19, pandaFile_.c_str());  // getVariable
            ASSERT_TRUE(location.GetMethodId().IsValid());
            ASSERT_SUCCESS(debugInterface->SetBreakpoint(location));
            return true;
        };

        breakpoint = [this](PtThread thread, Method *method, const PtLocation &location) {
            ASSERT_TRUE(CheckSetValues(thread, method, location.GetBytecodeOffset()));
            return true;
        };
    }

    std::pair<const char *, const char *> GetEntryPoint() override
    {
        return {pandaFile_.c_str(), entryPoint_.c_str()};
    }

    bool CheckSetValues(PtThread thread, Method *method, uint32_t offset)
    {
        // NOLINTNEXTLINE(modernize-avoid-c-arrays)
        std::function<bool(JSTaggedValue)> checkersList[] = {
            [&](JSTaggedValue value) {  // boolean: false
                ASSERT_EQ(value.GetRawData(), JSTaggedValue::VALUE_FALSE);
                return true;
            },
            [&](JSTaggedValue value) {  // boolean: true
                ASSERT_EQ(value.GetRawData(), JSTaggedValue::VALUE_TRUE);
                return true;
            },
            [&](JSTaggedValue value) {  // int: -2147483648 (INT_MIN)
                ASSERT_TRUE(value.IsInteger());
                ASSERT_EQ(value.GetDouble(), INT_MIN);
                return true;
            },
            [&](JSTaggedValue value) {  // int: 2147483647 (INT_MAX)
                ASSERT_TRUE(value.IsInteger());
                ASSERT_EQ(value.GetInt(), INT_MAX);
                return true;
            },
            [&](JSTaggedValue value) {  // double: 1.5
                ASSERT_TRUE(value.IsDouble());
                // NOLINTNEXTLINE(readability-magic-numbers)
                ASSERT_EQ(value.GetDouble(), double(1.5));  // 1.5
                return true;
            },
            [&](JSTaggedValue value) {  // String: "new_string"
                PandaString checkedValue;
                ASSERT_TRUE(value.IsHeapObject());
                auto vregValue = ecmascript::PtEcmaScriptExtension::TaggedValueToVRegValue(value);
                ASSERT_TRUE(ecmascript::JSTestApi::VRegValueToString(vregValue, &checkedValue));
                // NOLINTNEXTLINE(readability-string-compare)
                ASSERT_TRUE(checkedValue.compare("new_string") == 0);
                return true;
            }};

        ++countChecker_;
        uint32_t frameDepth = 0;
        int32_t argNumber = TestUtil::GetValueRegister(method, "bl1", offset);
        for (const auto &checker : checkersList) {
            VRegValue vregValue;
            ASSERT_SUCCESS(debugInterface->GetVariable(thread, frameDepth, argNumber++, &vregValue));
            ASSERT_TRUE(checker(ecmascript::PtEcmaScriptExtension::VRegValueToTaggedValue(vregValue)));
        }

        return true;
    }

private:
    std::string pandaFile_ = "js/GetVariable.abc";
    std::string entryPoint_ = "_GLOBAL::func_main_0";

    int countChecker_ = 0;
};

inline std::unique_ptr<ApiTest> GetJsGetVariableTest()
{
    return std::make_unique<JsGetVariableTest>();
}
}  // namespace ark::tooling::test

#endif  // PANDA_TOOLING_TEST_JS_GET_VARIABLE_TEST_H
